package maven.jyhs.com.servlet;

import com.alibaba.fastjson2.JSON;
import com.mysql.cj.util.StringUtils;
import maven.jyhs.com.entity.Menu;
import maven.jyhs.com.proxy.dynamic.ProxyFactory;
import maven.jyhs.com.servce.MenuSevrce;
import maven.jyhs.com.servce.impl.MenuSevrceImpl;
import maven.jyhs.com.util.Reflection;



import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



//@WebServlet(urlPatterns = "/menu")
public class MenuServlet extends HttpServlet {
    private MenuSevrce menuService = new MenuSevrceImpl();

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MenuSevrce mn = new MenuSevrceImpl();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        MenuSevrce m = ProxyFactory.create(new MenuSevrceImpl());
//        m.setDao(new MenuDaoImpl());
        String opr = null;
        if (request.getParameter("opr") != null) {
            opr = request.getParameter("opr");
        }
        String id = request.getParameter("id");
        String title = request.getParameter("ti");

        if (!StringUtils.isNullOrEmpty(title)) {
            int i = 0;
            try {
                i = m.deleteMenu(Integer.parseInt(id));
            } catch (ClassCastException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessError e) {
                throw new RuntimeException(e);
            }
            out.println(JSON.toJSONString(i));
        }
        if (opr.equals("sy")) {
            ArrayList<Menu> lists =  (ArrayList<Menu>)mn.getMenuList();
            List<Menu> menus =mn.getMenuList();
            Menu menu = new Menu();
            menu.setId(0);
            setChild(menu,menus);

            Map map = new HashMap();
            Map homeInfoMap = new HashMap();
            homeInfoMap.put("title", "首页");
            homeInfoMap.put("href", "page/welcome-1.html?t=1");
            Map logoInfoMap = new HashMap();
            logoInfoMap.put("title", "logo");
            logoInfoMap.put("href", "page/welcome-2.html?t=1");
            logoInfoMap.put("image", "images/logo.png");

            map.put("homeInfo", homeInfoMap);
            map.put("logoInfo", logoInfoMap);
            map.put("menuInfo", menu.getChild());
            System.out.println(JSON.toJSONString(map));

            String json = JSON.toJSONString(map);
            PrintWriter writer = response.getWriter();
            out.println(json);
            writer.flush();
            writer.close();

        } else {
            try {
                switch (opr) {
                    case "add":
                        addMenu(request, response);
                        break;
                    case "del":
                        deleteMenu(request, response);
                        break;
                    case "upd":
                        updateMenu(request, response);
                        break;
                    default:
                        getMenuList(request, response);
                        break;
                }
            } catch (ParseException e) {
                throw new RuntimeException(e);
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }

        out.flush();
        out.close();

    }
    public void setChild(Menu menu, List<Menu> list) {
        List<Menu> childList = new ArrayList<>();
        for (Menu menu1 : list) {
            if (menu1.getPid() == menu.getId()) {
                setChild(menu1, list);
                childList.add(menu1);
            }
        }
        menu.setChild(childList);
    }
    public void addMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException, IllegalAccessException {
        PrintWriter writer = response.getWriter();
        Menu menu = new Menu();
        Reflection.httpServletToObject(request, menu);
        writer.println(JSON.toJSONString(menuService.addMenu(menu)));
        writer.flush();
        writer.close();
    }

    public void deleteMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException, IllegalAccessException {
        int id = Integer.parseInt(request.getParameter("id"));
        PrintWriter writer = response.getWriter();
        writer.println(JSON.toJSONString(menuService.deleteMenu(id)));
        writer.flush();
        writer.close();
    }

    public void updateMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParseException, IllegalAccessException {
        PrintWriter writer = response.getWriter();
        Menu menu = new Menu();
        Reflection.httpServletToObject(request, menu);
        writer.println(JSON.toJSONString(menuService.updateMenu(menu)));
        writer.flush();
        writer.close();
    }

    public void getMenuList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter writer = response.getWriter();
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", "19");
        map.put("data", menuService.getMenuList());
        writer.println(JSON.toJSONString(map));
        writer.flush();
        writer.close();
    }



    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
